/**
 * @author VernHe
 * @date 2021年05月25日 14:03
 */



public class Solution_098 {
    public static void main(String[] args) {
        TreeNode node = new TreeNode(-2147483648);
//        node.left = new TreeNode(1);
        node.left = null;
        node.right = new TreeNode(2147483647);
        System.out.println(new Solution_098().isValidBST(node));
    }
    private static boolean stop;
    private static long pre = Long.MIN_VALUE;
    public boolean isValidBST(TreeNode root) {
        if (root == null || root.left == null && root.right == null) {
            return true;
        }
        stop = false;
        System.out.println("开始递归");
        isValid(root);
        return !stop;
    }

    //[5,4,6,null,null,3,7]
    public void isValid(TreeNode root) {
        if (root == null) {
            System.out.println("空节点");
            return;
        }
        System.out.println("当前节点:" + root.val + " ,stop: " +stop + " ,pre: " + pre);
        if (stop) {
            System.out.println("stop: " + stop);
            System.out.println("停止递归");
            return;
        }
        isValid(root.left);
        if (pre >= root.val) {
            System.out.println("发现不符合的");
            System.out.println("pre:" + pre + " ,cur:" + root.val);
            stop = true;
            return;
        }
        pre = root.val;
        isValid(root.right);
    }


    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
}
